import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/index'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css' // 默认主题
//拖转弹框
import 'babel-polyfill';

import config from './conf/config.js'

Vue.prototype.$extconf = config
Vue.config.productionTip = false;
Vue.use(ElementUI, {size: 'small'});

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
	let token = sessionStorage.getItem('api-token');
	//totken 不存在前往登录
	if (token && to.path === '/login') {next('/')}
	
	if (!token && to.path !== '/login') {
		next('/login')
	} else if(to.path !== '/login' && !store.state.hasResRouter) {
		// 判断是否加载过路由
		store.state.hasResRouter = true
		// 更新store中的token
		store.commit('setToken',token)  
		// let [e,route] = await store.dispatch('admin/menu');// 从接口获取用户路由并进行筛选
		// router.addRoutes(route);  //将筛选的动态加载到路由中
		// next({ ...to, replace: true })
	}
	next()
});

new Vue({
    router,
	store,
    render: h => h(App)
}).$mount('#app');
